<template>
  <div id="register">
    <!-- 图标固定在左上角 -->
    <div class="logo-container">
      <img src="http://121.43.196.119:9000/images/image.png" class="login-logo" alt="系统Logo">
    </div>

    <el-form ref="ruleFormRef" :model="registerForm" :rules="rules" label-width="80px" class="form-card">
      <div class="form-header">
        <h3>注册</h3>
        <p class="form-desc">请填写信息完成注册</p>
      </div>

      <el-form-item label="账号" prop="username">
        <el-input v-model="registerForm.username" placeholder="请设置账号" prefix-icon="User" class="form-input" />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="registerForm.password" type="password" show-password placeholder="请设置密码" prefix-icon="Lock"
          class="form-input" />
      </el-form-item>

      <div class="form-actions">
        <el-button type="success" @click="handleRegister()" class="register-btn" :loading="isLoading">
          <template #default>
            <span v-if="!isLoading">注册</span>
            <span v-else>注册中...</span>
          </template>
        </el-button>
        <el-button type="primary" @click="toLogin()" class="login-btn">
          已有账号？登录
        </el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { registerJS } from '@/api/user/user'
import router from '@/router'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

// 加载状态
const isLoading = ref(false)
const ruleFormRef = ref()

// 注册表单数据
const registerForm = reactive({
  username: "",
  password: ""
})

// 表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 10, message: '账号长度3~10', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 10, message: '密码长度3~10', trigger: 'blur' },
  ],
})

// 处理注册逻辑
const handleRegister = async () => {
  if (!ruleFormRef.value) return

  try {
    isLoading.value = true
    // 表单验证
    await ruleFormRef.value.validate()

    // 调用注册接口
    const res = await registerJS(registerForm)

    // 处理注册结果
    if (res.data === "注册成功") {
      ElMessage.success('注册成功，正在跳转到登录页...')
      setTimeout(() => {
        router.push("/")
      }, 800)
    } else {
      ElMessage.error(res.data || '注册失败，请重试')
    }
  } catch (error) {
    if (error.name === 'ValidationError') {
      ElMessage.warning('请检查输入的账号密码格式')
    } else {
      ElMessage.error('网络请求失败，请稍后再试')
      console.error('注册错误:', error)
    }
  } finally {
    isLoading.value = false
  }
}

// 跳转到登录页
const toLogin = () => {
  router.push("/")
}
</script>

<style scoped>
#register {
  background: url("https://omo-oss-image.thefastimg.com/portal-saas/new2022111121264844598/cms/image/cceaa502-be4f-4c5a-86cf-2a5c8db9665c.jpg") no-repeat center/cover;
  height: 100vh;
  width: 100%;
  position: fixed;
}

/* Logo样式保持与登录页一致 */
.logo-container {
  margin: 30px;
  position: relative;
  z-index: 10;
}

.login-logo {
  height: 50px;
  width: 200px;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.login-logo:hover {
  transform: scale(1.05);
}

/* 表单卡片大小与登录页统一 */
.form-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 0.92);
  padding: 30px 40px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 400px;
  backdrop-filter: blur(8px);
}

/* 表单头部样式 */
.form-header {
  text-align: center;
  margin-bottom: 25px;
}

.form-header h3 {
  font-size: 22px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-desc {
  color: #666;
  font-size: 14px;
  margin: 0;
}

/* 输入框样式统一 */
.form-input {
  width: 100%;
  height: 42px;
  border-radius: 6px;
}

/* 按钮区域样式 */
.form-actions {
  display: flex;
  gap: 15px;
  margin-top: 20px;
}

.register-btn,
.login-btn {
  flex: 1;
  height: 42px;
  font-size: 15px;
  transition: all 0.2s ease;
  border-radius: 6px;
}

.register-btn {
  background-color: #52c41a;
  border-color: #52c41a;
}

.register-btn:hover {
  background-color: #4aa919;
  border-color: #4aa919;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(82, 196, 26, 0.2);
}

.login-btn {
  background-color: #1890ff;
  border-color: #1890ff;
}

.login-btn:hover {
  background-color: #096dd9;
  border-color: #096dd9;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.2);
}

/* 响应式适配 */
@media (max-width: 576px) {
  .logo-container {
    margin: 20px 15px;
  }

  .login-logo {
    width: 160px;
    height: 40px;
  }

  .form-card {
    padding: 20px 25px;
    margin: 0 15px;
  }
}
</style>